package kz.kstu.website.entity.security;

import kz.kstu.website.entity.CommonEntityConstant;
import kz.kstu.website.entity.Identifiable;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;


import javax.persistence.*;
import java.util.Collection;
import java.util.Collections;

/**
 * @author Filipp_Stankevich
 *         created: 6/24/11 3:03 PM
 */

@NamedQuery(name = CommonEntityConstant.FIND_ACCOUNT_BY_USER_NAME, query = "select a from Account a where a.login = :userName")

@Entity
@Table(name = "ACCOUNT")
public class Account implements Identifiable, UserDetails {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    private Long id;

    @NotBlank
    @Column(name = "LOGIN", length = 50, unique = true)
    private String login;

    @NotBlank
    @Column(name = "PASSWORD", length = 50, nullable = false)
    private String password;

    @Email
    @NotBlank
    @Column(name = "EMAIL", length = 50)
    private String email;

    @Column(name = "IS_ACTIVE")
    private boolean isActive = true;

    @ManyToOne
    @JoinColumn(name = "ACCOUNT_ROLE_ID", referencedColumnName = "ID", nullable = false)
    private AccountRole role;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Collection<GrantedAuthority> getAuthorities() {
        return Collections.singleton((GrantedAuthority)role);
    }

    public String getPassword() {
        return password;
    }

    public String getUsername() {
        return login;
    }

    public boolean isAccountNonExpired() {
        return isActive;
    }

    public boolean isAccountNonLocked() {
        return isActive;
    }

    public boolean isCredentialsNonExpired() {
        return isActive;
    }

    public boolean isEnabled() {
        return isActive;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public boolean getActive() {
        return isActive;
    }

    public void setActive(boolean active) {
        isActive = active;
    }

    public AccountRole getRole() {
        return role;
    }

    public void setRole(AccountRole role) {
        this.role = role;
    }
}
